package edu.sjb.map;

import java.util.Random;

import edu.sjb.Drawable;
import edu.sjb.characters.Mario;
import edu.sjb.constants.ImageConstants;
import edu.sjb.map.prizes.Coin;
import edu.sjb.map.prizes.Mushroom;
import edu.sjb.map.prizes.Mushroom1UP;
import edu.sjb.map.prizes.NuclearBomb;
import edu.sjb.map.prizes.PowerUp;
import edu.sjb.map.prizes.Star;
import edu.sjb.threads.SoundThread;

public class PrizeMapBrick extends MapBrick {
	protected PowerUp item;
	protected boolean hasPowerUp;
	protected Random randomizer;

	public PrizeMapBrick() {
		super();
	}

	@Override
	public void hitAgainstMario(Mario m) {

		super.hitAgainstMario(m);
		if (hasPowerUp && m.posY() > posY()) {
			SoundThread.powerUp();
			hasPowerUp = false;
			image().changeImage(ImageConstants.BRICK_SOLID);
			item.image().setHidden(false);
		}

	}

	public void setHasPowerUp(boolean pw) {
		hasPowerUp = pw;
	}

	public void setPowerUp(PowerUp i) {
		item = i;
	}

	private PowerUp randomPowerUp(PrizeMapBrick brick) {

		PowerUp clonedPU = null;
		PowerUp newPU = null;
		randomizer = new Random();
		int randomInt = randomizer.nextInt(5);
		switch (randomInt) {
			case 0: {
				clonedPU = new Mushroom();
				newPU = (PowerUp) clonedPU.clonate();
				break;
			}
			case 1: {
				clonedPU = new Star();
				newPU = (PowerUp) clonedPU.clonate();
				break;
			}
			case 2: {
				clonedPU = new Mushroom1UP();
				newPU = (PowerUp) clonedPU.clonate();
				break;
			}
			case 3: {
				clonedPU = new Coin();
				newPU = (PowerUp) clonedPU.clonate();
				break;
			}
			case 4: {
				clonedPU = new NuclearBomb();
				newPU = (PowerUp) clonedPU.clonate();
				break;
			}
		}
		return newPU;
	}

	public PowerUp powerUp() {
		return item;
	}

	protected Object clone() {
		PrizeMapBrick newPUBrick = new PrizeMapBrick();
		newPUBrick.setPowerUp(newPUBrick.randomPowerUp(newPUBrick));
		Drawable nuevaImagen = null;
		nuevaImagen = new Drawable(ImageConstants.BRICK_QM, false);
		newPUBrick.setImage(nuevaImagen);
		newPUBrick.setHasPowerUp(true);
		return newPUBrick;
	}
}